package com.iot.platform.mvc.product.service.impl;

import java.util.ArrayList;
import java.util.Collections;

import org.springframework.stereotype.Service;

import com.iot.platform.api.domain.ProductCategory;
import com.iot.platform.mvc.product.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

	@Override
	public String queryCategoryName(String categoryId) {
		ArrayList<String> names = new ArrayList<String>();
		getNextCategory(categoryId, names);
		if (names.size() == 0) {
			return "";
		}
		Collections.reverse(names);
		return org.apache.commons.lang.StringUtils.join(names, "/");
	}

	private void getNextCategory(String categoryId, ArrayList<String> names) {
		ProductCategory productCategory = ProductCategory.dao.findById(categoryId);
		if (productCategory != null) {
			getNextCategory(productCategory.getId(), names);
			names.add(productCategory.getName());
		}
	}

}
